import requests
import json
import pprint
from top.starp.util import json_util
from top.starp.util import time_util
from top.starp.util import string_util

from top.starp.util import file_util
from top.starp.util import common
from top.starp.util import k



# file_util.filename_删掉括号
# file_util.to_file_path_name()
# python 字符串 转化为 json对象
# string_util.to 

# string_util. 




# json_util.str_to_json_obj 
# request_util
# HttpUtil

# payload = json.dumps(postData)
# headers = {
#    'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJqb2huc29uMjUyNSIsImlzcyI6InR5cmZpbmciLCJuYW1lIjoiam9obnNvbjI1MjXogIHluIgiLCJhcHBDb2RlIjoiNjQiLCJleHAiOjE2OTM4MDc5MDEsImlhdCI6MTY5Mzc4OTg0MSwic2lkIjoiNDAzMTMifQ.vYrC_qHicPb9TF7kZYlA-XCfYKQf5Zh1hoSryQJAuDQ',
#    'App-Code': '64',
#    'User-Agent': 'apifox/1.0.0 (https://www.apifox.cn)',
#    'Content-Type': 'application/json'
# }

def post(url,postData):
    yun_ying_token_obj=json_util.file_path_to_dict(common.yun_ying_token_path)
    token=yun_ying_token_obj.get(k.token)
    print("token",token)
    # token='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJqb2huc29uMjUyNSIsImlzcyI6InR5cmZpbmciLCJuYW1lIjoiam9obnNvbjI1MjXogIHluIgiLCJhcHBDb2RlIjoiNjQiLCJleHAiOjE2OTQxMDc3NjYsImlhdCI6MTY5NDA4OTcwNiwic2lkIjoiNDAzMTMifQ.BRQKzlLv7gHrVQChzU6a5xhBCdkuYSERLxDlk7YG474'
    # token='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJqb2huc29uMjUyNSIsImlzcyI6InR5cmZpbmciLCJuYW1lIjoiam9obnNvbjI1MjXogIHluIgiLCJhcHBDb2RlIjoiNjQiLCJleHAiOjE2OTQxMDg3NzQsImlhdCI6MTY5NDA5MDcxNCwic2lkIjoiNDAzMTMifQ.YaotOCZxf1r7PL9C3yedP7kQTk_jQmOTsa-SX05aeCE'
    # 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJqb2huc29uMjUyNSIsImlzcyI6InR5cmZpbmciLCJuYW1lIjoiam9obnNvbjI1MjXogIHluIgiLCJhcHBDb2RlIjoiNjQiLCJleHAiOjE2OTM4MDc5MDEsImlhdCI6MTY5Mzc4OTg0MSwic2lkIjoiNDAzMTMifQ.vYrC_qHicPb9TF7kZYlA-XCfYKQf5Zh1hoSryQJAuDQ'
    Authorization=f'Bearer {token}'
    print("Authorization",Authorization)
    # Authorization="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJqb2huc29uMjUyNSIsImlzcyI6InR5cmZpbmciLCJuYW1lIjoiam9obnNvbjI1MjXogIHluIgiLCJhcHBDb2RlIjoiNjQiLCJleHAiOjE2OTQxMDc3NjYsImlhdCI6MTY5NDA4OTcwNiwic2lkIjoiNDAzMTMifQ.BRQKzlLv7gHrVQChzU6a5xhBCdkuYSERLxDlk7YG474"
    headers = {
    'Authorization': Authorization,
    'App-Code': '64',
    'User-Agent': 'apifox/1.0.0 (https://www.apifox.cn)',
    'Content-Type': 'application/json'
    }
    return postDo(url,postData,headers)

    # payload = json.dumps(postData)
    # response = requests.request("POST", url, headers=headers, data=payload)
    # # print(response.text)
    # # print(response.json())
    # response_json=response.json()
    # pprint.pprint(response_json)
    # # H:\file\httpRes
    # req_data={
    #     "url":url,
    #     "postData":postData,
    #     "response_json":response_json
    # }
    # now_time_str=time_util.get_now_time_str()
    # out_dir=r'H:\file\httpRes'
    # url_str=file_util.to_file_path_name(url)
    # # json_util.json_to_file(req_data,r'D:\proj\python\my_util_py_pub\req_data.json')
    # json_util.json_to_file(req_data,rf'{out_dir}/req_data_{url_str}_{now_time_str}.json')
    # return response


def postDo(url,postData,headers):
    # yun_ying_token_obj=json_util.file_path_to_dict(common.yun_ying_token_path)
    # token=yun_ying_token_obj.get(k.token)
    # # 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJqb2huc29uMjUyNSIsImlzcyI6InR5cmZpbmciLCJuYW1lIjoiam9obnNvbjI1MjXogIHluIgiLCJhcHBDb2RlIjoiNjQiLCJleHAiOjE2OTM4MDc5MDEsImlhdCI6MTY5Mzc4OTg0MSwic2lkIjoiNDAzMTMifQ.vYrC_qHicPb9TF7kZYlA-XCfYKQf5Zh1hoSryQJAuDQ'
    # headers = {
    # 'Authorization': f'Bearer {token}',
    # 'App-Code': '64',
    # 'User-Agent': 'apifox/1.0.0 (https://www.apifox.cn)',
    # 'Content-Type': 'application/json'
    # }

    payload = json.dumps(postData)
    response = requests.request("POST", url, headers=headers, data=payload)
    # print(response.text)
    # print(response.json())
    response_json=response.json()
    pprint.pprint(response_json)
    # H:\file\httpRes
    req_data={
        "url":url,
        "postData":postData,
        "response_json":response_json
    }
    now_time_str=time_util.get_now_time_str()
    # out_dir=r'H:\file\httpRes'
    out_dir=rf'D:\file\httpRes'
    url_str=file_util.to_file_path_name(url)
    # json_util.json_to_file(req_data,r'D:\proj\python\my_util_py_pub\req_data.json')
    json_util.json_to_file(req_data,rf'{out_dir}/req_data_{url_str}_{now_time_str}.json')
    return response_json



def postNoToken(url,postData,withAppCode=True):
    # yun_ying_token_obj=json_util.file_path_to_dict(common.yun_ying_token_path)
    # token=yun_ying_token_obj.get(k.token)
    headers = {
       "Origin": "https://pkqa.com.cn",
    # 'User-Agent': 'apifox/1.0.0 (https://www.apifox.cn)',
    'User-Agent':  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69",
    'Content-Type': 'application/json'
    }
    if withAppCode:
        headers['App-Code']="64"
    return postDo(url,postData,headers)

    # payload = json.dumps(postData)
    # response = requests.request("POST", url, headers=headers, data=payload)
    # # print(response.text)
    # # print(response.json())
    # response_json=response.json()
    # pprint.pprint(response_json)
    # # H:\file\httpRes
    # req_data={
    #     "url":url,
    #     "postData":postData,
    #     "response_json":response_json
    # }
    # now_time_str=time_util.get_now_time_str()
    # out_dir=r'H:\file\httpRes'
    # url_str=file_util.to_file_path_name(url)
    # # json_util.json_to_file(req_data,r'D:\proj\python\my_util_py_pub\req_data.json')
    # json_util.json_to_file(req_data,rf'{out_dir}/req_data_{url_str}_{now_time_str}.json')
    # return response
